package com.lcc.gulimall.product.service.impl;

import com.lcc.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.lcc.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.lcc.gulimall.product.entity.AttrEntity;
import com.lcc.gulimall.product.service.AttrAttrgroupRelationService;
import com.lcc.gulimall.product.service.AttrService;
import com.lcc.gulimall.product.vo.AttrGroupDescVo;
import com.lcc.gulimall.product.vo.AttrGroupRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcc.common.utils.PageUtils;
import com.lcc.common.utils.Query;

import com.lcc.gulimall.product.dao.AttrGroupDao;
import com.lcc.gulimall.product.entity.AttrGroupEntity;
import com.lcc.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    AttrAttrgroupRelationService relationService;
    @Autowired
    AttrService attrService;
    @Autowired
    AttrAttrgroupRelationDao relationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        if(catelogId == 0){//查所有
            String key = (String) params.get("key");
            QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(key)){
                queryWrapper.eq("attr_group_id", key).or().like("attr_group_name", key);
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }else {
           String key = (String) params.get("key"); //获取检索条件
            // select * from pms_attr_group where catelog_id = catelogId and (attr_group_id=key or attr_group_name like %key%)
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>()
                    .eq("catelog_id", catelogId);
            if(!StringUtils.isEmpty(key)){
                wrapper.and(obj->{
                    obj.eq("attr_group_id", key)
                            .or().like("attr_group_name", key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 删除分组与属性的关联关系
     * @param vos
     */
    @Override
    public void deleteRelation(AttrGroupRelationVo[] vos) {
//        relationDao.delete(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", vos).eq("attr_group_id", vos));
        List<AttrAttrgroupRelationEntity> collect = Arrays.asList(vos).stream().map(item -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        //完成批量删除
        relationDao.deleteBatchRelation(collect);
    }

    @Override
    public List<AttrGroupDescVo> listAttrGroupWithAttrByCatId(Long catelogId, int attrType) {
        //1.查询catelogId的所有属性分组
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //2.查询所有属性分组下的属性，封装返回
        List<AttrGroupDescVo> descVos = groupEntities.stream().map(
                item -> {
                    AttrGroupDescVo descVo = new AttrGroupDescVo();
                    BeanUtils.copyProperties(item, descVo);
                    List<AttrAttrgroupRelationEntity> attrIds = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>()
                            .eq("attr_group_id", item.getAttrGroupId()));
                    List<AttrEntity> attrs = new ArrayList<>();
                    for (AttrAttrgroupRelationEntity entity : attrIds) {
                        AttrEntity attrEntity = attrService.getOne(new QueryWrapper<AttrEntity>().eq("attr_id", entity.getAttrId()).eq("attr_type", attrType));
                        if(attrEntity != null){
                            attrs.add(attrEntity);
                        }
                    }
                    descVo.setAttrs(attrs);
                    return descVo;
                }
        ).collect(Collectors.toList());
        return descVos;
    }

}